package io.renren.api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.api.entity.*;
import io.renren.api.service.*;
import io.renren.api.vo.LunBoVo;
import io.renren.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping("/api")
public class DianPuPageController {
    @Autowired
    private BtCustomerNavbarService btCustomerNavbarService;
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;
    @Autowired
    private BtCustomerProBaseinfoService btCustomerProBaseinfoService;
    @Autowired
    private BtCustomerMessageService btCustomerMessageService;
    @Autowired
    private BtCustomerFirstpageService btCustomerFirstpageService;
    @Autowired
    private BtCustomerSummaryService btCustomerSummaryService;

    @RequestMapping("/dianpu/{id}")
    public String firstPage(@PathVariable("id") Long id,Map<String, Object> backdata) {
        BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.selectById(id);
        if(btCustomerBaseEntity == null){
            return "404";
        }
        backdata.putAll(getHandInfo(btCustomerBaseEntity));
        //首页内容
        BtCustomerFirstpageEntity btCustomerFirstpageEntity = btCustomerFirstpageService.GetOnebyCustId(btCustomerBaseEntity.getId());
        btCustomerFirstpageEntity.setLunboimgs(btCustomerFirstpageEntity.getImgLink().split(";"));
        backdata.put("shouyecontent",btCustomerFirstpageEntity);
        //线路汇总
        List<BtCustomerProBaseinfoEntity> cutsxianlu= btCustomerProBaseinfoService.selectList(
                new EntityWrapper<BtCustomerProBaseinfoEntity>().eq("cust_id",btCustomerBaseEntity.getId())
        );
        List<Map<String,Object>> dianpukecheng=new ArrayList<>();
        for(BtCustomerProBaseinfoEntity vustprobase :cutsxianlu){
            Map<String,Object> temp =new HashMap<>();
            temp.put("name",vustprobase.getTitle());
            temp.put("link","detail/"+vustprobase.getId());
            temp.put("zsdx",vustprobase.getZsdxDesc());
            temp.put("zbcs",vustprobase.getZbcsDesc());
            temp.put("kcts",vustprobase.getKcts());
            temp.put("kcjg",vustprobase.getKcjg());
            dianpukecheng.add(temp);
        }
        backdata.put("dianpukecheng",dianpukecheng);
        //精彩瞬间
        List<Map<String,Object>> dianpujcxj=new ArrayList<>();
        List<BtCustomerMessageEntity> cutsjingcailist = btCustomerMessageService.selectList(
                new EntityWrapper<BtCustomerMessageEntity>().setSqlSelect("id,typestr,title,img").eq("typestr","jcxj").eq("cust_id",btCustomerBaseEntity.getId())
        );
        for(BtCustomerMessageEntity vustjcxj :cutsjingcailist){
            Map<String,Object> temp =new HashMap<>();
            temp.put("name",vustjcxj.getTitle());
            temp.put("img",vustjcxj.getImg());
            temp.put("link","msgdetail/"+vustjcxj.getId());
            dianpujcxj.add(temp);
        }
        backdata.put("dianpujcxj",dianpujcxj);
        return "shop/index";
    }
    @RequestMapping("/dianpu/{id}/typestr/{tstr}")
    public String TypeStr(@PathVariable("id") Long id,@PathVariable("tstr") String tstr,Map<String, Object> backdata) {
        BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.selectById(id);
        if(btCustomerBaseEntity == null){
            return "404";
        }
        backdata.putAll(getHandInfo(btCustomerBaseEntity));
        String backurl="";
        PageUtils page;
        Map<String, Object> params = new HashMap<>();
        params.put("limit","25");
        params.put("page","1");
        params.put("order","asc");
        switch (tstr){
            //简介
            case "jj":
                BtCustomerSummaryEntity btCustomerSummaryEntity = btCustomerSummaryService.selectOne(new EntityWrapper<BtCustomerSummaryEntity>().eq("cust_id",btCustomerBaseEntity.getId()));
                backdata.put("content",btCustomerSummaryEntity.getContext());
                backurl="shop/jianjie";
            break;
            //常见问题
            case "cjwt":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerMessageService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/changjianwenti";
                break;
            //游学游记
            case "yxyj":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerMessageService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/youxueyouji";
                break;
            //营地风采
            case "ydfc":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerMessageService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/yingdifengcai";
                break;
            //精彩瞬间
            case "jcxj":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerMessageService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/jingcaishunjian";
                break;
            //
            case "yyzx":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerMessageService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/jianjie";
                break;
            //线路汇总
            case "xlhz":
                params.put("tstr",tstr);
                params.put("cust_id",btCustomerBaseEntity.getId());
                page = btCustomerProBaseinfoService.queryPage(params);
                backdata.put("contentlist",page);
                backurl="shop/xianluhuizong";
                break;
        }
        return backurl;
    }
    @RequestMapping("/dianpu/{id}/cata/{cid}")
    public String Cata(@PathVariable("id") Long id,@PathVariable("cid") Long cid,Map<String, Object> backdata) {
        BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.selectById(id);
        if(btCustomerBaseEntity == null){
            return "404";
        }
        backdata.putAll(getHandInfo(btCustomerBaseEntity));
        Map<String, Object> params = new HashMap<>();
        params.put("limit","25");
        params.put("page","1");
        params.put("order","asc");
        params.put("cataid",cid);
        params.put("cust_id",btCustomerBaseEntity.getId());
        PageUtils page = btCustomerProBaseinfoService.queryPage(params);
        backdata.put("contentlist",page);
        return "shop/";
    }
    private Map<String, Object> getHandInfo(BtCustomerBaseEntity btCustomerBaseEntity){
        Map<String, Object> backdata = new HashMap<>();
        //1店铺页的头部
        backdata.put("dianputou",btCustomerBaseEntity);
        //2左边信息获得
        //2.1线路列表
        List<BtCustomerProBaseinfoEntity> cutsbastlist = btCustomerProBaseinfoService.selectList(
                new EntityWrapper<BtCustomerProBaseinfoEntity>().setSqlSelect("id,title").eq("cust_id",btCustomerBaseEntity.getId())
        );
        List<Map<String,Object>> dianpuleftxianlu=new ArrayList<>();
        for(BtCustomerProBaseinfoEntity vustprobase :cutsbastlist){
            Map<String,Object> temp =new HashMap<>();
            temp.put("name",vustprobase.getTitle());
            temp.put("link","detail/"+vustprobase.getId());
            dianpuleftxianlu.add(temp);
        }
        backdata.put("dianpuleftxianlu",dianpuleftxianlu);
        //2.2常见问题
        List<Map<String,Object>> dianpuleftcjwt=new ArrayList<>();
        List<BtCustomerMessageEntity> cutscjwtlist = btCustomerMessageService.selectList(
                new EntityWrapper<BtCustomerMessageEntity>().setSqlSelect("id,typestr,title,summary").eq("typestr","cjwt").eq("cust_id",btCustomerBaseEntity.getId())
        );
        for(BtCustomerMessageEntity vustcjwt :cutscjwtlist){
            Map<String,Object> temp =new HashMap<>();
            temp.put("name",vustcjwt.getTitle());
            temp.put("summary",vustcjwt.getSummary());
            temp.put("link","msgdetail/"+vustcjwt.getId());
            dianpuleftcjwt.add(temp);
        }
        backdata.put("dianpuleftcjwt",dianpuleftcjwt);
        //2.3精彩瞬间
        List<Map<String,Object>> dianpuleftjcxj=new ArrayList<>();
        List<BtCustomerMessageEntity> cutsjcxjlist = btCustomerMessageService.selectList(
                new EntityWrapper<BtCustomerMessageEntity>().setSqlSelect("id,typestr,title,summary").eq("typestr","jcxj").eq("cust_id",btCustomerBaseEntity.getId())
        );
        for(BtCustomerMessageEntity vustjcxj :cutsjcxjlist){
            Map<String,Object> temp =new HashMap<>();
            temp.put("name",vustjcxj.getTitle());
            temp.put("summary",vustjcxj.getSummary());
            temp.put("link","msgdetail/"+vustjcxj.getId());
            dianpuleftcjwt.add(temp);
        }
        backdata.put("dianpuleftjcxj",dianpuleftjcxj);
        //获取导航栏
        List<Map<String,Object>> dianpubarlist=new ArrayList<>();
        List<BtCustomerNavbarEntity> custbarlist=btCustomerNavbarService.selectList(new EntityWrapper<BtCustomerNavbarEntity>()
                .eq("pid",0).eq("cust_id",btCustomerBaseEntity.getId()));
        for(BtCustomerNavbarEntity vustbase :custbarlist){
            Map<String,Object> temp =new HashMap<>();
            if(vustbase.getTypestr().equals("sy")){
                temp.put("name",vustbase.getName());
                temp.put("link","/api/dianpu/"+vustbase.getCustId());
            }else if(vustbase.getIsHassub().equals("1")){
                temp.put("name",vustbase.getName());
                List<BtCustomerNavbarEntity> subcustbarlist=btCustomerNavbarService.selectList(new EntityWrapper<BtCustomerNavbarEntity>()
                        .eq("pid",vustbase.getId()).eq("cust_id",btCustomerBaseEntity.getId()));
                if(subcustbarlist.size()>0){
                    List<Map<String,Object>> subdianpubarlist=new ArrayList<>();
                    for(BtCustomerNavbarEntity svustbase :subcustbarlist){
                        Map<String,Object> stemp =new HashMap<>();
                        stemp.put("name",svustbase.getName());
                        stemp.put("link","/api/dianpu/"+vustbase.getCustId()+"/cata/"+svustbase.getId());
                        subdianpubarlist.add(stemp);
                    }
                    temp.put("sublist",subdianpubarlist);
                }
                else{
                    temp.put("link","/api/dianpu/"+vustbase.getCustId()+"/cata/"+vustbase.getId());
                }
            }else{
                temp.put("name",vustbase.getName());
                temp.put("link","/api/dianpu/"+vustbase.getCustId()+"/typestr/"+vustbase.getTypestr());
            }
            dianpubarlist.add(temp);
        }
        backdata.put("custbarlist",dianpubarlist);
        return backdata;
    }
}
